package com.atguigu.juc.day04.cf;

import com.atguigu.juc.day04.pool.Demo01MyPool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Demo13Handle {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // handle() 方法：其中设定的回调方法必然被调用
        // 前面任务正常：接收前面任务的结果，throwable 为 null
        // 前面任务异常：前面任务结果为 null，throwable 就是前面任务抛出的异常
        String finalTaskResult =
                CompletableFuture
                        .supplyAsync(() -> {
                            System.out.println(10 / 0);
                            return "task 01 result";
                        }, Demo01MyPool.getExecutorService())
                        .handle((prevTaskResult, throwable) -> {
                            System.out.println("prevTaskResult = " + prevTaskResult);
                            System.out.println("throwable = " + throwable);
                            return "task result from handle";
                        }).get();

        System.out.println("finalTaskResult = " + finalTaskResult);

    }

}
